package com.kingyun.gpsinspection.purificationservice.services.activitiservice.ext.listener;

import com.kingyun.gpsinspection.purificationservice.common.activiti.ApplicationContextUtils;
import com.kingyun.gpsinspection.purificationservice.common.enums.LeaveEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.LeaveMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.MakeHolidayMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.Leave;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.MakeHoliday;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.history.HistoricProcessInstance;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 自定义全局监听器
 */
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CustomerExcutionListener implements ExecutionListener {

    private static final String END_ID = "endevent1";

    private static final String KEY = "leave";

    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        String currentActivityId = delegateExecution.getCurrentActivityId();
        String processBusinessKey = delegateExecution.getProcessBusinessKey();
        String processDefinitionId = delegateExecution.getProcessDefinitionId();
        if (END_ID.equals(currentActivityId) && processDefinitionId.contains(KEY)) {
            //扣减不休假和年假天数
            LeaveMapper leaveMapper = (LeaveMapper) ApplicationContextUtils.getBean(LeaveMapper.class);
            MakeHolidayMapper makeHolidayMapper = (MakeHolidayMapper) ApplicationContextUtils.getBean(MakeHolidayMapper.class);
            List<HistoricProcessInstance> list = delegateExecution.getEngineServices().getHistoryService().createHistoricProcessInstanceQuery().
                    processInstanceId(delegateExecution.getProcessInstanceId()).list();
            if (list == null && list.size() < 1) {
                throw new RuntimeException("流程实例数据有误");
            }
            Leave leave = leaveMapper.selectByPrimaryKey(processBusinessKey);
            if (leave == null) {
                throw new RuntimeException("不存在相关的业务数据");
            }
            if (LeaveEnum.MARKERD.getCode() == leave.getType()) { //补假相关扣减
                String month = leave.getBeginTime();
                Date date = DateUtils.parseDate(month, "yyyy-MM-dd HH:mm");
                HistoricProcessInstance historicProcessInstance = list.get(0);
                Integer days = leave.getDays().intValue();
                String employeeId = historicProcessInstance.getStartUserId();
                List<MakeHoliday> makeHolidays = makeHolidayMapper.queryRemianDaysOfMonth(DateFormatUtils.format(date, "yyyy-MM"), employeeId);
                if (makeHolidays != null && makeHolidays.size() > 0) {
                    Integer remianDays = 0;
                    for (MakeHoliday makeHoliday : makeHolidays) {
                        remianDays = makeHoliday.getRemainDays() - days;
                        if (remianDays >= 0) {
                            MakeHoliday updateMakeHoliday = new MakeHoliday();
                            updateMakeHoliday.setEmployeeId(employeeId);
                            updateMakeHoliday.setRemainDays(remianDays);
                            makeHolidayMapper.updateByEmployeeId(updateMakeHoliday);
                            break;
                        } else {
                            days = Math.abs(remianDays);
                            MakeHoliday updateMakeHoliday = new MakeHoliday();
                            updateMakeHoliday.setEmployeeId(employeeId);
                            updateMakeHoliday.setRemainDays(0);
                            makeHolidayMapper.updateByEmployeeId(updateMakeHoliday);
                        }
                    }

                }

            }
        }

    }
}
